package de.gameduell.framework.facebook {
	import de.gameduell.framework.locale.LocaleData;
	import de.gameduell.framework.gui.util.Formatter;

	import flash.events.TimerEvent;
	import flash.utils.Timer;

	import de.gameduell.facebook.service.gen.UserProfile;
	import de.gameduell.facebook.service.gen.JackPotGame;
	import de.gameduell.framework.debug.Debug;

	import flash.events.Event;
	import flash.events.EventDispatcher;

	public class Jackpot extends EventDispatcher {
		
		public static const REMAINING_TIME_UPDATE:String = "REMAINING_TIME_UPDATE";
		public static const JACKPOT_CHANGED:String = "JACKPOT_CHANGED";
		public static const JACKPOT_LEADER_CHANGED:String = "JACKPOT_LEADER_CHANGED";
		public static const JACKPOT_CLOSED:String = "JACKPOT_CLOSED";
		public static const RESULT_REJECTED:String = "RESULT_REJECTED";
		public static const JACKPOT_WON:String = "JACKPOT_WON";
		
		public static const JACKPOT_TIME:int = 600000;
		
		private const UPDATE_INTERVAL:int = 10000;
		
		private var _remainingTime:int;
		private var _jackpot:JackPotGame;
		private var _currentJackpotId:String;
		private var _jackpotLeader:UserProfile;
		private var controller:FBBaseController;
		private var jackpotTimer:Timer;
		private var localeData:LocaleData;
		private var _currentJackpotOpen:Boolean;

		public function Jackpot() {
			_remainingTime = JACKPOT_TIME;
			_currentJackpotId = "";
			localeData = LocaleData.getInstance();
			_currentJackpotOpen = true;
		}
		
		public function init(aController:FBBaseController):void {
			controller = aController;
			jackpotTimer = new Timer(UPDATE_INTERVAL);
			jackpotTimer.addEventListener(TimerEvent.TIMER, controller.getJackPotGame);
			jackpotTimer.addEventListener(TimerEvent.TIMER, updateRemainingTimeValues);
		}
		
		public function initJackpotGame():void {
			controller.getJackPotGame();
			jackpotTimer.start();
		}
		
		private function updateRemainingTimeValues(event:Event = null):void {
			Debug.trace("Jackpot::updateRemainingTimeValues", Debug.CONNECTION);
			controller.communication.requestGetJackpotTimeLeftInSeconds();
		}
		
		public function set jackpot(aJackpot:JackPotGame):void {
			Debug.trace("Jackpot::set jackpot", Debug.CONNECTION);
			_jackpot = aJackpot;
			
			localeData.setValue("leaderId", "" + jackpot.leaderId);
			localeData.setValue("jackpotScore", Formatter.formatNumberEnglish("" + jackpot.highscore));
			localeData.setValue("currentJackpot", Formatter.formatNumberEnglish("" + jackpot.potsize));
			
			dispatchEvent(new Event(JACKPOT_CHANGED));
			if(!_currentJackpotOpen){
				dispatchEvent(new Event(JACKPOT_CLOSED));
				_currentJackpotOpen = true;
				jackpotTimer.delay = UPDATE_INTERVAL;
				controller.communication.requestGetCurrentJackPotGame(); // get a new jackpot
			}
			if(!jackpotTimer.running) {
				Debug.trace("Jackpot::set jackpot START TIMER", Debug.CONNECTION);
				jackpotTimer.start();
			}
		}

		public function get jackpot():JackPotGame {
			return _jackpot;
		}
	
		public function set currentJackpotId(anId:String):void {
			_currentJackpotId = anId;
		}

		public function get currentJackpotId():String {
			return _currentJackpotId;
		}
		
		public function set jackpotLeader(aLeader:UserProfile):void {
			_jackpotLeader = aLeader;
			Debug.trace("Jackpot::set jackpotLeader: firstName = " + _jackpotLeader.firstName, Debug.DEBUG);
			Debug.trace("Jackpot::set jackpotLeader: image = " + _jackpotLeader.image, Debug.DEBUG);
			dispatchEvent(new Event(JACKPOT_LEADER_CHANGED));
		}
		
		public function get jackpotLeader():UserProfile {
			return _jackpotLeader;
		}
		
		public function set remainingTime(timeLeft:int):void {
			_remainingTime = timeLeft *  1000;
			Debug.trace("Jackpot::remainingTime: " + _remainingTime + " leaderId: " + _jackpot.leaderId, Debug.WARNING);
			dispatchEvent(new Event(REMAINING_TIME_UPDATE));
			if(_remainingTime <= UPDATE_INTERVAL && _remainingTime > 0) {
				jackpotTimer.delay = 5000;
				Debug.trace("Jackpot::remainingTime: resetting delay to -> " + 5000, Debug.DEBUG);
			}else if(jackpotTimer.delay != UPDATE_INTERVAL){
				jackpotTimer.delay = UPDATE_INTERVAL;
			}
			if(_remainingTime <= 0){
				Debug.trace("Jackpot::remainingTime: TIME's UP!!!", Debug.DEBUG);
				handleClosedJackpot();
			}
		}
		
		public function handleClosedJackpot():void {
			Debug.trace("Jackpot::handleClosedJackpot: JACKPOT CLOSED ###############", Debug.CONNECTION);
			jackpotTimer.delay = UPDATE_INTERVAL;
			jackpotTimer.stop();
			_currentJackpotOpen = false;
			controller.getJackPotGame(); // get the final result
		}
		
		public function handleResultForClosedJackpot():void {
			Debug.trace("Jackpot::handleResultForClosedJackpot: RESULT NOT SENT ... JACKPOT ALREADY CLOSED", Debug.WARNING);
			dispatchEvent(new Event(RESULT_REJECTED));	
		}

		public function get remainingTime():int {
			return _remainingTime;
		}
		
		public function get currentJackpotOpen():Boolean {
			return _currentJackpotOpen;
		}
	}
}
